const { Transform } = require('stream');
const _ = require('lodash');
const moment = require('moment');
const utils = require('../../utils');

module.exports = function (inFormat) {
  const stream = new Transform({ objectMode: true });
  inFormat = utils.parseFunction(inFormat);
  stream._transform = async function (chunk, encoding, callback) {
    utils.debug('mapTransform [chunk]:', chunk);
    const line = inFormat ? !_.isFunction(inFormat) ? _.get(chunk, inFormat) : await inFormat(chunk, { _, moment, utils }) : chunk;
    stream.push(line);
    callback();
  };
  stream.on('finish', function () {
    utils.debug('mapTransform [finish]');
  });
  return stream;
};